<?php
//积分商城
class inman_api_integralShop extends inman_api_base {
    public function __construct($app){
        $this->app=$app;
        $this->db = kernel::database();
    	$this->memberModel=kernel::single('b2c_mdl_members');
    	$this->memberCouponModel=kernel::single('b2c_mdl_member_coupon');
    	$this->couponsModel=kernel::single('b2c_mdl_coupons');
    }
    //积分商城首页
    public function index($params,&$service){
    	if(isset($params['sid'])){//参数检测
			$member_id=$this->check_sid($params['sid']);
			if(!$member_id){
           	 	return $service->send_user_error('100001','sid fail');
        	}else{
        		//获取积分
        		$point=$this->memberModel->getRow('point',array('member_id'=>$member_id));
        		$return['user_integral']=$point['point'];
        		$return['image']='商城首页图片';
        		$return['rule']='规则';
        		return $return;
        	}
    	}
    	
    }
    
    //获取商品列表
    public function getList($params,&$service){
    	if(isset($params['sid'])){//参数检测
    		$member_id=$this->check_sid($params['sid']);
    		if(!$member_id){
    			return $service->send_user_error('100001','sid fail');
    		}else{
    			$coupons_1=array();
    			$coupons_2=array();
    			/**
    			 * 获取所有的优惠券(启用,且积分不能为空,必须是b类优惠券,促销必须按照订单总价格减去固定的优惠价格)  初步筛选条件!需要进一步确认 
    			**/
    			$c_template='b2c_promotion_conditions_order_subtotalallgoods';
    			$s_template='b2c_promotion_solutions_byfixed';
    			$sql="select a.* from sdb_b2c_coupons as a inner join sdb_b2c_sales_rule_order as b on a.rule_id=b.rule_id  where a.cpns_type='1' and a.cpns_status='1' and  a.cpns_point is not null and a.cpns_type='1' and b.c_template='{$c_template}' and b.s_template='{$s_template}'";
    			$datas=$this->db->select($sql);
    			if($datas){
    				//需要进一步进行筛选(当前的时间在有效期之内,)
    				foreach ($datas as $data) {
    					$rules=kernel::single('b2c_mdl_sales_rule_order')->getRow('conditions,action_conditions,action_solution,from_time,to_time',array('rule_id'=>$data['rule_id']));
    					$time=time();
    					if($rules['from_time']>$time||$rules['to_time']<$time){ //根据时间过滤优惠券
    						continue;
    					}else{
    						$coupons_1[]=$data;
    					}
    				}
    			}
    			
    			/**
    			 * 获取所有的现金劵(启用,且积分不能为空,必须是b类优惠券,)  初步筛选条件!需要进一步确认
    			 **/
//     			$c_template='b2c_promotion_conditions_order_allorderallgoods';
//     			$s_template='b2c_promotion_solutions_byfixed';
//     			$sql="select a.* from sdb_b2c_coupons as a inner join sdb_b2c_sales_rule_order as b on a.rule_id=b.rule_id  where a.cpns_type='1' and a.cpns_status='1' and  a.cpns_point is not null and a.cpns_type='1' and b.c_template='{$c_template}' and b.s_template='{$s_template}'";
//     			$datas=$this->db->select($sql);
//     			if($datas){
//     				//需要进一步进行筛选(当前的时间在有效期之内,)
//     				foreach ($datas as $data) {
//     					$rules=kernel::single('b2c_mdl_sales_rule_order')->getRow('conditions,action_conditions,action_solution,from_time,to_time',array('rule_id'=>$data['rule_id']));
//     					$time=time();
//     					if($rules['from_time']>$time||$rules['to_time']<$time){ //根据时间过滤优惠券
//     						continue;
//     					}else{
//     						$coupons_2[]=$data;
//     					}
//     				}
//     			}
    			//获得所有的积分兑换劵
    			$coupons=array_merge($coupons_1,$coupons_2);
    			//组装数据
    			foreach ($coupons as $key=>$coupon){
    				//根据rule_id获取使用时间
    				$rules=kernel::single('b2c_mdl_sales_rule_order')->getRow('s_template,c_template,conditions,action_conditions,action_solution,from_time,to_time',array('rule_id'=>$coupon['rule_id']));
    				//积分兑换劵的类型
    				if($rules['c_template']=='b2c_promotion_conditions_order_allorderallgoods'){
    					$type=2;
    				}else{
    					$type=1;
    				}
    				$return[$key]['shop_id']=$coupon['cpns_id'];
    				$return[$key]['type']=$type;
    				$return[$key]['integral_price']=$coupon['cpns_point'];
    				$return[$key]['number']='剩余数量';
    				$return[$key]['amount_price']=$rules['action_solution']['b2c_promotion_solutions_byfixed']['total_amount'];
    				$return[$key]['lowest_amount']=$rules['conditions']['conditions'][1]['conditions'][0]['value'];
    				$return[$key]['start_time']=$rules['from_time'];
    				$return[$key]['end_time']=$rules['to_time'];
    				$return[$key]['goods_desc']=$coupon['cpns_name'];
//     				$return[$key]['c_template']=$rules['c_template'];
//     				$return[$key]['s_template']=$rules['s_template'];
    			}
    			return $return;
    		}
    	}
    }
//     shop_id	int	ID
//     type	int	1优惠券,2现金券,3实物
//     integral_price	int	兑换所需积分
//     number	int	剩余数量
//     amount_price	float	面额
//     lowest_amount	float	使用门槛
//     start_time	int	有效期开始时间
//     end_time	int	有效期期结束时间
//     goods_desc	string	商品描述

	//兑换
	public function exchange($params,&$service){
		if(isset($params['sid'])){//参数检测
			$member_id=$this->check_sid($params['sid']);
			if(!$member_id){
				$error['error_code']=1; //sid错误
				return $error; 
			}else{
				if(!isset($params['shop_id'])||$params['shop_id']==null){
					$error['error_code']=1; //参数错误
					return $error; 
				}
				//获取配置文件积分设置的用途
				$site_point_usage = app::get('b2c')->getConf('site.point_usage');
				if($site_point_usage != '1'){
					$error['error_code']=1; //积分只用于抵扣，不能兑换
					return $error;
				}
				//获取当前用户的积分...
				$point=kernel::single('b2c_mdl_members')->getRow('point',array('member_id'=>$member_id));
				//进行积分兑换
				$cpnsId=$params['shop_id'];//优惠价id
				$cur_coupon_nums = $this->memberCouponModel->count(array('cpns_id'=>$cpnsId,'member_id'=>$member_id));//当前用户的优惠券数量
				$coupons = $this->couponsModel;
				$cur_coupon = $coupons->dump($cpnsId);
// 				[cpns_id] => 3
// 				[cpns_name] => 我的优惠券
// 				[pmt_id] =>
// 				[cpns_prefix] => Bsd23r5
// 				[cpns_gen_quantity] => 1
// 				[cpns_key] => jY6ImNvdXB
// 				[cpns_status] => 1
// 				[cpns_type] => 1
// 				[cpns_point] => 2
// 				[rule] => Array
// 				(
// 						[rule_id] => 4
// 				)
				
// 				[cpns_max_num] => 2
// 				)
				if($cur_coupon['cpns_max_num'] > 0 ){  //兼容老数据处理老数据还是无限制兑换
					if($cur_coupon_nums >= $cur_coupon['cpns_max_num']){
						$error['status']='false';
						$error['message']=$this->app->_('您的兑换次数已达上限');
						return $error;
					}
				}
				if($point['point']< $cur_coupon['cpns_point']){
					$error['status']='false';
					$error['message']=$this->app->_('您的积分不足！');
					return $error;
				}
				$oExchangeCoupon = kernel::single('b2c_coupon_mem');//在该类中进行积分操作
				if ($oExchangeCoupon->exchange($cpnsId,$member_id,$point['point'],$params)){  //将优惠券写入用户优惠券表 
// 					[direct] => true
// 					[sid] => be203d2723e078877bc8b2e86f92cf77
// 					[shop_id] => 3
// 					[cpns_point] => 100
// 					[memc_code] => Bsd23r5EEFD600004
					$cpns_point = $params['cpns_point'];
					$member_point =app::get('b2c')->model('member_point');
					//修改用户的积分,添加修改积分记录
					if($member_point->change_point($member_id,-$cpns_point,$msg,'exchange_coupon',2,$member_id,$member_id,'exchange')){
						$change_nums = $cur_coupon['cpns_max_num'] - $cur_coupon_nums -1;
						if($cur_coupon['cpns_max_num'] > 0 ){
							$error['status']='false';
							$error['message']=$this->app->_('兑换成功,您还可以兑换'.$change_nums.'张');
							return $error;
						}
						else{
							$error['status']='false';
							$error['message']=$this->app->_('兑换成功');
							return $error;
						}
					}else{
						$oExchangeCoupon->exchange_delete($params);//删除添加在“用户优惠券表”的数据
						$error['status']='false';
						$error['message']=$this->app->_('兑换失败');
						return $error;
					}
				}
				
			}
		}

		
		
	}
}